<template>
  <div id="login">
    <div class="login-box">
      <!-- 猫头鹰 -->
      <div class="owl" id="owl">
        <!-- hand:手 -->
        <div class="hand"></div>
        <div class="hand hand-r"></div>
        <!-- 手臂 -->
        <div class="arms">
          <div class="arm"></div>
          <div class="arm arm-r"></div>
        </div>
      </div>
      <!-- 输入表单 -->
      <el-form v-model="loginData" ref="loginDataRef" class="input-box">
        <el-input v-model="loginData.username" size="large" placeholder="请输入用户名" style="margin-bottom: 5px" />
        <el-input @keydown.enter.native="login" show-password v-model="loginData.password" @focus="x" @blur="y" size="large" placeholder="请输入密码" id="password" style="margin-bottom: 5px" />
        <el-button @click="login">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>

import {ref} from "vue";
import {ElNotification} from "element-plus"
import axios from "axios";
import md5 from "js-md5"
import {useRouter} from "vue-router"

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Login",
  setup() {
    const router = useRouter()
    let loginData = ref({
      username: "",
      password: ""
    })
    let loginDataRef = ref()
    const x = () => {
      $('#owl').addClass('password');
    }
    const y = () => {
      $('#owl').removeClass('password');
    }
    const login = () => {
      if (loginData.value.username === '' || loginData.value.password === '') {
        ElNotification({
          title: '错误',
          message: '用户名或密码不能为空',
          type: 'error'
        })
      } else {
        let loginMd5Data = {
          username: JSON.parse(JSON.stringify(loginData.value.username)),
          password: md5(JSON.parse(JSON.stringify(loginData.value.password)))
        }
        // 验证用户名密码
        loginVerify(loginMd5Data)
      }
    }

    const loginVerify = (loginMd5Data) => {
      axios.post("/api/login",loginMd5Data).then(res=>{
        if (res.data.code === 200) {
          localStorage.setItem("token", res.data.data)
          localStorage.setItem("username", loginData.value.username)
          router.push("/home")
        } else {
          loginData.value.password = ""
          ElNotification({
            title: '错误',
            message: res.data.msg,
            type: 'error'
          })
        }
      })
    }

    return {
      x,y,login,loginData,loginDataRef
    }
  }
}
</script>

<style scoped>
*{
  /*初始化*/
  margin: 0;
  padding: 0;
}
#login{
  /* 100%窗口高度 */
  height: 100vh;
  /* 弹性布局，居中 */
  display: flex;
  justify-content: center;
  align-items: center;
  /* 渐变背景 */
  background: linear-gradient(200deg,#72afd3,#157e44);
}
.login-box{
  /* 相对定位 */
  position: relative;
  width: 320px;
}
.input-box{
  display: flex;
  flex-direction: column;
}
.input-box input{
  height: 40px;
  border-radius: 3px;
  /* 文字缩进15px */
  text-indent: 15px;
  /* outline绘制元素周围线的形状 */
  outline: none;
  /* 边框样式 */
  border: none;
  margin-bottom: 15px;
}
.input-box input:focus{
  outline: 1px solid lightseagreen;
}
.input-box button{
  border: none;
  height: 45px;
  background-color: lightseagreen;
  color: #fff;
  border-radius: 3px;
  cursor: pointer;
}
/* 接下来是猫头鹰的样式 */
.owl{
  width: 211px;
  height: 108px;
  /* 背景图片猫头鹰的头 */
  background: url("~@/assets/images/owl-login.png") no-repeat;
  position: absolute;
  top: -100px;
  left: 50%;
  transform: translateX(-50%);
}
.owl .hand{
  /* 自己画两个圆充当猫头鹰的手 */
  width: 34px;
  height: 34px;
  border-radius: 40px;
  background-color: #472d20;
  position: absolute;
  left: 12px;
  bottom: -8px;
  /* 沿着y轴缩放0.6倍（压扁） */
  transform: scaleY(0.6);
  /* ease-out:规定以慢速结束的过渡效果,完成该动画需要0.3s */
  transition: 0.3s ease-out;
}
.owl .hand.hand-r{
  left: 170px;
}
/* 定义密码焦点触发时猫头鹰手的样式 */
.owl.password .hand{
  transform: translateX(42px) translateY(-15px) scale(0.7);
}
.owl.password .hand.hand-r{
  transform: translateX(-42px) translateY(-15px) scale(0.7);
}
.owl .arms{
  position: absolute;
  top: 58px;
  width: 100%;
  height: 41px;
  overflow: hidden;
}
/* 猫头鹰的手臂(翅膀) */
.owl .arms .arm{
  width: 40px;
  height: 65px;
  position: absolute;
  left: 20px;
  top: 40px;
  background: url("~@/assets/images/owl-login-arm.png") no-repeat;
  transform: rotate(-20deg);
  transition: 0.3s ease-out;
}
.owl .arms .arm.arm-r{
  transform: rotate(20deg) scaleX(-1);
  left: 158px;
}
/* 定义密码焦点触发时猫头鹰手臂(翅膀)的样式 */
.owl.password .arms .arm{
  transform: translateY(-40px) translateX(40px);
}
.owl.password .arms .arm.arm-r{
  transform: translateY(-40px) translateX(-40px) scaleX(-1);
}
</style>